#include "verifyruleview.h"
#include "ui_verifyruleview.h"
#include "changepasswordview.h"

VerifyRuleView::VerifyRuleView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VerifyRuleView)
{
    ui->setupUi(this);
    setWindowTitle("验证权限");
    userRule = Rule::NotLogin;
    uuid = -1;
}

VerifyRuleView::~VerifyRuleView()
{
    userRule = Rule::NotLogin;
    delete ui;
}

Rule VerifyRuleView::getUserRule()
{
    return userRule;
}

int VerifyRuleView::getUserID()
{
    return uuid;
}

void VerifyRuleView::on_okButton_clicked()
{
    QString userName = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();
    if (userName.isEmpty() || password.length() < 8 || password.length() > 12)
    {
        return;
    }
    DBOperate db;
    AccountInfo accountInfo = db.selectAccountInfo(userName, password);
    userRule = accountInfo.rule;
    this->uuid = accountInfo.uuid;
    if ((userRule != Rule::NotLogin) && accountInfo.firstLogin == 0)
    {
        ChangePasswordView changePasswordView(this);
        changePasswordView.setModal(true);
        changePasswordView.setID(uuid);
        changePasswordView.show();
        if (changePasswordView.exec() != QDialog::Accepted)
        {
            if (!changePasswordView.getSuccessState())
            {
                this->userRule = Rule::NotLogin;
                this->uuid = -1;
                return;
            }
        }
    }
    close();
}

void VerifyRuleView::setUserName(QString userName)
{
    ui->lineEdit->setText(userName);
    ui->lineEdit->setReadOnly(true);
}
